package com.acme.domain.account;

import com.acme.exceptions.*;

public class CheckingAccount extends AbstractAccount {

	private double overdraft;

	public CheckingAccount(final int id, final double amount,
			final double overdraft) {
		super(id, amount);
		this.overdraft = overdraft;
	}

	@Override
	public void withdraw(final double amount) throws NotEnoughFundsException  {
		if (amount < 0) {
			throw new IllegalArgumentException("Amount can not be negative");
		}
		if (this.balance - amount >= -overdraft) {
			this.balance -= amount;
		} else {
			throw new OverDraftLimitExceededException(this.balance+this.overdraft, this.toString());
		}

		if (this.balance < amount) {
			double overdraftNeeded = amount - this.balance;
			if (overdraft < overdraftNeeded) {
				throw new OverDraftLimitExceededException(this.balance+this.overdraft, this.toString());
			} else {
				this.balance = 0.0;
				overdraft -= overdraftNeeded;
			}
		} else {
			this.balance = this.balance - amount;
		}
	}

	@Override
	public void deposit(final double amount) {
		if (amount > 0) {
			this.balance += amount;
		} else {
			throw new IllegalArgumentException ("ammount should be positive");
		}
	}


	public AccountType getAccountType() {
		return AccountType.CHECKING;
	}

	public double getOverdraft() {
		return overdraft;
	}

	@Override
	public String toString() {
		return "CheckingAccount [" + id + "]";
	}
		
}
